package com.travel.server.manager.impl.dal;

import com.travel.common.exception.WorkCode;
import com.travel.common.exception.WorkException;
import com.travel.common.util.CollectionUtil;
import com.travel.server.manager.dao.OrderRefundInfoDao;
import com.travel.server.manager.model.OrderRefundInfo;
import com.travel.server.manager.model.OrderRefundInfoExample;
import com.travel.server.manager.model.dto.data.RefundAnalysisDto;
import com.travel.server.manager.model.dto.data.TopRefundDto;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @Author: LiZhenhui
 * @Date: 2018/11/5 18:26
 * @Desc:
 */
@Component
@Slf4j
public class RefundDal {

    @Autowired
    private OrderRefundInfoDao orderRefundInfoDao;

    public OrderRefundInfo selectById(Integer id){
        OrderRefundInfo refundInfo = orderRefundInfoDao.selectByPrimaryKey(id);
        if(refundInfo == null){
            log.error("未查询到退款记录, id {}", id);
            throw new WorkException(WorkCode.RECORD_NOT_EXIST);
        }
        return refundInfo;
    }

    public OrderRefundInfo selectByOrderId(Integer orderId){
        OrderRefundParam param = new OrderRefundParam();
        param.setOrderIdList(Arrays.asList(orderId));
        return CollectionUtil.getFirst(selectByExample(param));
    }

    public Integer insertOrUpdate(OrderRefundInfo orderRefundInfo){
        if(orderRefundInfo.getId() == null){
            orderRefundInfoDao.insertSelective(orderRefundInfo);
        }else{
            orderRefundInfoDao.updateByPrimaryKey(orderRefundInfo);
        }
        return orderRefundInfo.getId();
    }

    public List<OrderRefundInfo> selectByExample(OrderRefundParam param){
        OrderRefundInfoExample example = makeExample(param);
        return orderRefundInfoDao.selectByExample(example);
    }

    public List<RefundAnalysisDto> selectRefundAnalysis(Date startTime, Date endTime){
        return orderRefundInfoDao.selectRefundAnalysis(startTime, endTime);
    }

    public List<TopRefundDto> selectTopRefund(Integer num){
        return orderRefundInfoDao.selectTopRefund(num);
    }

    public Integer selectTotalRefund(){
        return orderRefundInfoDao.selectTotalRefund();
    }

    public String selectMaxReason(Integer projectId){
        return orderRefundInfoDao.selectMaxReason(projectId);
    }

    private OrderRefundInfoExample makeExample(OrderRefundParam param){
        OrderRefundInfoExample example = new OrderRefundInfoExample();
        OrderRefundInfoExample.Criteria criteria  = example.or();
        if(!CollectionUtil.isEmpty(param.getOrderIdList())){
            criteria.andOrderIdIn(param.getOrderIdList());
        }
        if(StringUtils.isNotBlank(param.getRefundId())){
            criteria.andRefundIdEqualTo(param.getRefundId());
        }
        if(param.getOffset() != null && param.getLimit() != null){
            example.setOffset(param.getOffset());
            example.setLimit(param.getLimit());
        }
        return example;
    }

    @Data
    public static class OrderRefundParam{

        private String refundId;

        private Integer offset;

        private Integer limit;

        private List<Integer> orderIdList;
    }
}
